package com.group3.hospital.controller;

import com.group3.hospital.bean.Admin;
import com.group3.hospital.bean.Doctor;
import com.group3.hospital.service.PasswordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author 尚硅谷
 * @create 2022/1/4
 */
@Controller
public class PasswordController {
    @Autowired
    private PasswordService passwordService;

    @RequestMapping("passwordUi")
    public String psswordUi() {

        return "/html/passwordUpdate";
    }


    @RequestMapping("update")
    public String update(long adminTel, String adminPassword, String newPassword1, String newPassword2) {
        String hisPad = null;
        try {
            hisPad = passwordService.getPassword(adminTel);
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<Long> tel = passwordService.getTel();
        for (Long aLong : tel) {
            if (adminTel == aLong) {
                if (!hisPad.equals(adminPassword)) {
//            System.out.println("密码不正确");
                    return "redirect:/paderror1Ui";
                } else {
                    if (!newPassword1.equals(newPassword2)) {
//                System.out.println("输入的两次新密码必须一直");
                        return "redirect:/paderror2Ui";
                    } else {
                        Admin admin = new Admin();
                        admin.setAdminTel(adminTel);
                        admin.setAdminPassword(newPassword1);
                        try {
                            passwordService.update(admin);
//                            System.out.println("修改成功了");
                            return "redirect:/Admin";
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            return "redirect:/paderror3Ui";
        }
//        if (!hisPad.equals(adminPassword)){
////            System.out.println("密码不正确");
//            return "redirect:/paderror1Ui";
//        } else{
//            if (!newPassword1.equals(newPassword2)){
////                System.out.println("输入的两次新密码必须一直");
//                return "redirect:/paderror2Ui";
//            } else{
//                Admin admin = new Admin();
//                admin.setAdminTel(adminTel);
//                admin.setAdminPassword(newPassword1);
//                try {
//                    passwordService.update(admin);
//                    System.out.println("修改成功了");
//                    return "redirect:/Admin";
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        }
        return "redirect:/html/passwordUpdate";
    }

    @RequestMapping("paderror1Ui")
    public String paderror1Ui() {
        return "/html/paderror1";
    }

    @RequestMapping("paderror2Ui")
    public String paderror2Ui() {
        return "/html/paderror2";
    }

    @RequestMapping("paderror3Ui")
    public String paderror3Ui() {
        return "/html/paderror3";
    }


}
